<?php

declare(strict_types=1);

namespace App\RoomMessageBatchSend\Controller\Dashboard;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use App\Common\Middleware\DashboardAuthMiddleware;
use Base\Controller\AbstractController;
use Base\Request\ValidateSceneTrait;
use App\RoomMessageBatchSend\Logic\IndexLogic;

/**
 * 客户群消息群发 - 消息列表.
 * @Controller
 */
class Index extends AbstractController
{
    use ValidateSceneTrait;

    /**
     * @var RequestInterface
     */
    protected $request;

    /**
     * @var IndexLogic
     */
    private $indexLogin;

    public function __construct(RequestInterface $request, IndexLogic $indexLogic)
    {
        $this->indexLogin = $indexLogic;
        $this->request = $request;
    }

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class)
     * })
     * @RequestMapping(path="/dashboard/roomMessageBatchSend/index", methods="post,get")
     */
    public function handle(): array
    {
        ## 参数验证
        $this->validated($this->request->all());
        ## 接收参数
        $params = [
            'page' => $this->request->input('current', 1),
            'perPage' => $this->request->input('size', 10),
        ];

        $batchTitle = $this->request->input('batchTitle', '');
        if (! empty($batchTitle)) {
            $params['batchTitle'] = $batchTitle;
        }
        $params['corpId'] = $this->request->input('userInfo.corp_id');
        $params['tenantId'] = $this->request->input('userInfo.tenant_id');
        $params['userId'] = $this->request->input('userInfo.user_id');
        $params['deptId'] = $this->request->input('userInfo.dept_id');
        return $this->indexLogin->handle($params);
    }

    /**
     * 验证规则.
     *
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [
            'current' => 'integer|min:1',
            'size' => 'integer',
        ];
    }

    /**
     * 验证错误提示.
     * @return array 响应数据
     */
    protected function messages(): array
    {
        return [];
    }
}
